package com.sxz.sbs.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sxz.sbs.R;

/**
 * 水平方向cell
 * @author tian
 */

public class HorizontalCellView extends LinearLayout {
    private ImageView ivLeft;
    private ImageView next;
    private TextView tvLeft;
    private TextView tvRight;
    private View line;
    private String leftText;
    private String rightText;
    private int rightTextColor;
    private boolean leftIvShow;
    private boolean lineShow;
    private int leftResId;
    private Context context;
    private boolean nextShow;
    public HorizontalCellView(Context context) {
        super(context);
    }
    public HorizontalCellView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        // 加载布局
        LayoutInflater.from(context).inflate(R.layout.horizontal_cell_layout, this);
        ivLeft = (ImageView) findViewById(R.id.cell_left_iv);
        tvLeft = (TextView) findViewById(R.id.cell_left_tv);
        tvRight = (TextView) findViewById(R.id.cell_right_tv);
        line =  findViewById(R.id.cell_line);
        next = (ImageView) findViewById(R.id.cell_right_iv);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.HorizontalCellView);
        leftText = ta.getString(R.styleable.HorizontalCellView_left_text);
        rightText = ta.getString(R.styleable.HorizontalCellView_right_text);
        rightTextColor = ta.getColor(R.styleable.HorizontalCellView_right_textColor, Color.BLACK);
        leftIvShow = ta.getBoolean(R.styleable.HorizontalCellView_left_img_show,true);
        lineShow = ta.getBoolean(R.styleable.HorizontalCellView_line_show,true);
        leftResId = ta.getResourceId(R.styleable.HorizontalCellView_left_img,android.R.drawable.sym_def_app_icon);
        nextShow = ta.getBoolean(R.styleable.HorizontalCellView_next_show,true);
        ta.recycle();  //注意回收
        initView();
    }
    public HorizontalCellView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

    }

    private void initView() {
        ivLeft.setImageResource(leftResId);
        if (leftIvShow) {
            ivLeft.setVisibility(VISIBLE);
        } else {
            ivLeft.setVisibility(GONE);
        }

        tvLeft.setText(leftText);
        tvRight.setText(rightText);
        tvRight.setTextColor(rightTextColor);
        if (!nextShow) {
            next.setVisibility(GONE);
        }
        if (!lineShow) {
            line.setVisibility(GONE);
        }
    }
    public void setRightText (String rightText) {
        tvRight.setText(rightText);
    }
    public String getRightText () {
        return tvRight.getText().toString();
    }
    /**
     * dp转px
     * @param dpValue dp值
     * @return px值
     */
    public  int dp2px(final float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}
